using System.Threading.Tasks;
using MalwareMultiScan.Api.Services.Interfaces;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace MalwareMultiScan.Api.Controllers
{
    /// <summary>
    /// Downloads controller.
    /// </summary>
    [ApiController]
    [Route("api/download")]
    [Produces("application/octet-stream")]
    public class DownloadController : Controller
    {
        private readonly IScanResultService _scanResultService;

        /// <summary>
        /// Initialize downloads controller.
        /// </summary>
        /// <param name="scanResultService">Scan result service.</param>
        public DownloadController(IScanResultService scanResultService)
        {
            _scanResultService = scanResultService;
        }

        /// <summary>
        /// Download file by id.
        /// </summary>
        /// <param name="id">File id.</param>
        [HttpGet("{id}")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        public async Task<IActionResult> Index(string id)
        {
            var fileStream = await _scanResultService.ObtainFile(id);

            if (fileStream == null)
                return NotFound();

            return File(fileStream, "application/octet-stream");
        }
    }
}